package top.porchwood.jweb.mcsm.controller;

import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import top.porchwood.jweb.mcsm.MCSMApplication;

import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.IOException;

@Controller
public class SystemStatusController {
    @RequestMapping("/status/disk")
    private void userGetSystemDiskStatus(HttpServletResponse response) throws IOException {
        response.setStatus(200);
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(new Gson().toJson(getDiskInfo()));
    }

    public static DiskPart[] getDiskInfo() {
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File[] disks = File.listRoots();
        DiskPart[] diskParts = new DiskPart[disks.length];
        String dataPath = new File(MCSMApplication.getConfig().getFilePath()).getAbsolutePath();
        for (int i = 0; i < disks.length; i++) {
            diskParts[i] = new DiskPart(
                    disks[i].getFreeSpace(),
                    disks[i].getUsableSpace(),
                    disks[i].getTotalSpace(),
                    disks[i].getPath(),
                    fsv.getSystemDisplayName(disks[i]),
                    dataPath.startsWith(disks[i].getPath().substring(0, 1))
            );
        }
        return diskParts;
    }

    private record DiskPart(long free, long usable, long total, String signal, String name, boolean isDataDisk) {
    }
}
